<?php

/**
 * 后台用户组管理
 * @author  xiaobai
 * @date    2017-01-12
 */

namespace app\panel\controller;

use think\Controller;
use app\common\controller\Panelbase;
use app\common\model\Group as group_model;

class Group extends Panelbase
{

    public function initialize()
    {
        parent::initialize();
        $this->model         = new group_model();
        $this->validate_name = 'app\common\validate\Group';
    }

    public function index()
    {
        $list = $this->model->order('sort asc,id desc')->select();

        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {        
        return $this->fetch();
    }

    public function add_post()
    {
        if (request()->isPost()) {
            $data = input('post.');

            // 数据验证
            $result = $this->validate($data, $this->validate_name);
            if (true !== $result) {
                $this->error($result);
            }
            
            if ($this->model->save($data)) {
                $this->success('保存成功', Url('index'));
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id = input("param.id");
        $row = $this->model->where('id',$id)->find()->append(['status_text'])->toArray();
        if (empty($row)) {
            $this->error('数据不存在');
        }
        
        $this->assign('row',$row);

        return $this->fetch();
    }

    public function edit_post()
    {
        if (request()->isPost()) {
            $data = input('post.');
            // 数据验证
            $result = $this->validate($data, $this->validate_name);
            if (true !== $result) {
                $this->error($result);
            }

            if ($this->model->where(['id'=>$data['id']])->update($data) !== false) {
                $this->success('保存成功', Url('index'));
            } else {
                $this->error($this->model->getError());
            }
        } else {
            $this->error('请求错误');
        }
    }

    public function authorize()
    {
        $id = input('param.id', 0, 'intval');
        if($id <= 0){
            $this->error("请选择要配置权限的用户组");
        }
        $group_menu = db('GroupMenu');
        $menu = model('menu');

        $rules = $group_menu->where('groupid='.$id)->column('menuid');
        
        $list = $menu->field('id,parentid,name,sort')->order("sort asc,id asc")->select();
        $list = json_decode(json_encode($list),true);
        $temp_List = array();
        foreach($list as $k=>$v){
            $temp_List[$v['id']] = $v;
            $temp_List[$v['id']]['checked'] = (in_array($v['id'],$rules))? 'checked' : '';                
        }
        
        $this->assign('temp_List', $temp_List);
        $this->assign('id', $id);

        return $this->fetch();
    }

    public function authorize_post()
    {
        $res = array();
        if(!request()->isPost()){
            $res['error'] = 1;
            $res['msg'] = '对不起，没有提交数据！';
            echo json_encode($res);die;
        }
        $id = input('param.gid', 0, 'intval');
        $menuid = input('param.purview');
        $group_menu = model('GroupMenu');

        if($id <= 0){
            $res['error'] = 1;
            $res['msg'] = '配置权限用户组错误！';
            echo json_encode($res);die;
        }
        if(!isset($menuid) || is_null($menuid)){
            $res['error'] = 1;
            $res['msg'] = '请选择要配置的权限';
            echo json_encode($res);die;
        }

        $menuid = substr($menuid,0,-1);
        $menu_arr = explode(',',$menuid);

        if(!is_array($menu_arr) && count($menu_arr) < 0){
            $res['error'] = 1;
            $res['msg'] = '请选择要配置的权限';
            echo json_encode($res);die;
        }

        $data = array();
        $group_menu->where("groupid=".$id)->delete();
        foreach($menu_arr as $k=>$v){
            $data[$k]['groupid'] = $id;
            $data[$k]['menuid'] = $v;
        }
        $res = $group_menu->saveAll($data);
        if ($res !== false) {
            $res['error'] = 0;
            $res['msg'] = '权限分配成功！';
            $res['url'] = Url('index');
            echo json_encode($res);die;
        } else {
            $res['error'] = 1;
            $res['msg'] = '系统繁忙，请稍后再试~';
            echo json_encode($res);die;
        }
    }
}
